<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>员工列表</title>

    <link rel="stylesheet" th:href="@{/layui/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/layuimini.css?v=2.0.4.2}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/themes/default.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body{
            padding: 20px;
        }
    </style>
</head>
<body>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">员工姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realname" required  lay-verify="required" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="1" title="男">
            <input type="radio" name="sex" value="2" title="女" checked>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" required  lay-verify="required|number" placeholder="请输入员工年龄" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">工资</label>
        <div class="layui-input-block">
            <input type="text" name="money" required  lay-verify="required|number" placeholder="请输入员工工资" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">生日</label>
        <div class="layui-input-block">
            <input type="text" name="birthday" required  lay-verify="required" placeholder="请输入员工生日" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <select name="deptId" lay-verify="required">
                <option value="">请选择</option>
                <option value="">100</option>
                <option th:each="dept:${deptList}" th:value="${dept.deptId}">
                    <span th:text="${dept.deptName}"></span>
                </option>

            </select>
        </div>
    </div>


    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">家庭地址</label>
        <div class="layui-input-block">
            <textarea name="address" placeholder="请输入家庭地址" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn-submit" class="layui-btn" lay-submit lay-filter="btn-submit-filter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

    <script th:src="@{/layui/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
    <script th:src="@{/layui/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
    <script th:inline="javascript">
        var ctxPath = [[@{/}]];
        /*日期控件*/
        // 导入laydate，利用laydate对象，执行render的实例
        layui.use(['laydate','table','form'], function(){
            var laydate = layui.laydate;
            var table = layui.table;
            var form = layui.form;
            // 提交
            var $ = layui.$;

            // 日期控件
            laydate.render({
                elem: '[name="birthday"]'   // 指定元素
            });

            // 表单提交
            form.on('submit(btn-submit-filter)', function(data){
                // 提交按钮无效化
                $("#btn-submit").attr("disabled","disabled").addClass("layui-btn-disabled");
                // 异步请求提交数据
                var url = ctxPath + "emp";
                $.post(url,data.field,function (response) {
                    if (response.code == 0){
                        // 成功提示
                        layer.msg(response.message, {
                            icon: 1,
                            time: 2000  //2秒关闭（如果不配置默认3秒）
                        }, function () {
                            // 刷新表格
                            parent.refreshTable();
                            // 关闭窗口
                            var index = parent.layer.getFrameIndex(window.name);    // 先得到当前iframe层的索引
                            parent.layer.close(index);  // 再执行关闭
                        });
                    }else{
                        $("#btn-submit").removeAttr("disabled","disabled").removeClass("layui-btn-disabled");
                        // 失败提示信息，窗口不关闭
                        layer.msg("新增失败，请联系管理员", {
                            icon: 2,
                            anim: 6
                        });
                    }
                });
                // 提交成功则关闭窗口，并刷新数据表格
                // 失败则提示错误信息，窗口不关闭

                // return false;   //阻止表单跳转。如果需要表单跳转，去掉这段即可

            });
        });
    </script>
</body>
</html>